package com.zmwx.auth.application.controller;


import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.zmwx.auth.application.convert.AuthPermissionDTOToConverter;
import com.zmwx.auth.application.convert.AuthRolePermissionDTOToConverter;
import com.zmwx.auth.application.dto.AuthPermissionDTO;
import com.zmwx.auth.application.dto.AuthRolePermissionDTO;
import com.zmwx.auth.common.entity.Result;
import com.zmwx.auth.domain.entityDO.AuthPermissionBO;
import com.zmwx.auth.domain.entityDO.AuthRolePermissionBO;
import com.zmwx.auth.domain.service.AuthPermissionDomainService;
import com.zmwx.auth.domain.service.AuthRolePermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 角色权限Controller
 */
@RestController
@RequestMapping("/rolePermission")
@Slf4j
public class RolePermissionController {

    @Resource
    private AuthPermissionDomainService authPermissionDomainService;
    @Resource
    private AuthRolePermissionDomainService authRolePermissionDomainService;

    /**
     * 新增权限关联
     * @param authPermissionDTO
     * @return
     */
    @RequestMapping("/add")
    public Result<Boolean> addPermission(@RequestBody AuthRolePermissionDTO authRolePermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("authRolePermissionDTO:{}", authRolePermissionDTO);
            }
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId(), "角色ID不能为空");
           // Preconditions.checkNotNull(authRolePermissionDTO.getPermissionId(), "权限KEY 不能为空");
            AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionDTOToConverter.INSTANCE.convertDTOToBo(authRolePermissionDTO);
            boolean result = authRolePermissionDomainService.addRolePermission(authRolePermissionBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("新增权限失败error:{}", e.getMessage(), e);
            return Result.fail("新增权限失败");
        }
    }

}

